<?php

declare(strict_types=1);

namespace App\Http\Model\BankAcount;

use Illuminate\Support\Facades\DB;
use App\Http\Library\Excel;
use App\Http\ModelDao\BankAcount\BankEntrydataVerifyowDao;

class BankEntrydataVerifyModel
{

/*数据集合*/
    private static $rows;
    /*数据长度*/
    private static $rowsCount = 0;
    /*
     * @param array $params
     *
     * return array
     */
    public static function index($params = []):array{

            $limit =  (int)$params["pagesize"];
            $offset = ((int)$params["page"]-1)*$limit;
            $where = [];
            /*城市名称*/
            if(!isEmpty($params["city_name"])){
                $where[] = ["city_name","=",$params["city_name"]];
            }
            /*主体名称*/
            if(!isEmpty($params["ledger_subject_name"])){
                $where[] = ["ledger_subject_name","=",$params["ledger_subject_name"]];
            }
            /*监管银行账号*/
            if(!isEmpty($params["account"])){
                $where[] = ["account","=",$params["account"]];
            }
            /*汇总时间-区间*/
             if(!isEmpty($params["collect_ctime"]) && !isEmpty($params["collect_etime"])){
                $where[] = ["transaction_date",">=",$params["collect_ctime"]];
                $where[] = ["transaction_date","<=",$params["collect_etime"]];
            }
            //DB::connection()->enableQueryLog();#开启执行日志

            //download select
            if($params["download"] == 'All'){
                /*获取全部数据*/
                self::$rows = DB::table("bank_entrydata_verify")->wherein('pfs_dw_id',$params['pfs_dw_id'])->where($where)->orderBy("id","desc")->get();
                self::$rowsCount = sizeof(self::$rows);
            }else{
                self::$rows = DB::table("bank_entrydata_verify")->wherein('pfs_dw_id',$params['pfs_dw_id'])->where($where)->orderBy("id","desc")->offset($offset)->limit($limit)->get();
                self::$rowsCount = DB::table("bank_entrydata_verify")->wherein('pfs_dw_id',$params['pfs_dw_id'])->where($where)->count();
            }

            //print_r(DB::getQueryLog());   //获取查询语句、参数和执行时间

        self::download($params);

        return [
            'list' => self::$rows,
            'allRows' => self::$rowsCount,
        ];
    }
    /*
     * 数据下载
    */
    private static function download($params){
        //
        if(in_array($params["download"],['All','Current'])){
            Excel::$download = true;
            Excel::$rowWidth = [
                'A:A' => 20,
                'B:B' => 12,
                'C:C' => 12,
                'D:D' => 14,
                'E:E' => 16,
                'F:F' => 14,
                'G:G' => 21,
                'H:H' => 14,
                'I:I' => 14,
                'J:J' => 11,
            ];
            $title = ['交易时间','城市','主体','监管账号','当日收款总额','当日退款总额','当日应转监管户金额','银行实收金额','银行实收日期','差异金额'];
            //$title = ['a','b','c','d'];
            Excel::$title = $title;

           //var_dump(ClassTransactionDetailDao::numberResult(self::$rows));exit();
            Excel::send(BankEntrydataVerifyowDao::numberResult(self::$rows),'银行入账数据核对-'.date("Ymd-His").'.xlsx');
             exit();
        }
    }
}
